package com.fourth.controller.api;

import com.fourth.service.UbCommentService;
import com.github.pagehelper.PageInfo;
import com.ljx.entity.Comment;
import com.ljx.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Mr.ma
 * @since 2023/3/2 14:36
 **/
@RestController
@RequestMapping("/detail/comment")
@Api("评论操作接口")
public class UbApiCommentController {
    @Resource
    private UbCommentService ubCommentService;

    @RequestMapping("findComment/{id}/{pageCurrent}/{pageSize}")
    @ApiOperation("查询评论信息，含分页以及条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageCurrent", value = "页码", dataType = "int" , defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", dataType = "int" , defaultValue = "5"),
            @ApiImplicitParam(name = "id", value = "商品id", dataType = "String"),
    })
    public Result<PageInfo<Comment>> selectAll(@PathVariable("id") String id,
                                               @PathVariable("pageCurrent") Integer pageCurrent,
                                               @PathVariable("pageSize")Integer pageSize){
        PageInfo<Comment> comments = ubCommentService.selectAll(pageCurrent,pageSize,id);
        return new Result<>(2000,"查询成功",comments);
    }

    @RequestMapping("addComment")
    @ApiOperation("插入评论信息")
    @ApiImplicitParam(name = "comment", value = "评论信息", dataType = "Comment")
    public Result addComment(@RequestBody Comment comment){
        int row = ubCommentService.insert(comment);
        if (row > 0){
            return new Result(2000,"插入成功",null);
        }
        return new Result(5000,"插入失败",null);
    }

    @RequestMapping("updateComment")
    @ApiOperation("修改评论信息")
    @ApiImplicitParam(name = "comment", value = "评论信息", dataType = "Comment")
    public Result updateComment(@RequestBody Comment comment){
        int row = ubCommentService.updateComment(comment);
        if (row > 0){
            return new Result(2000,"修改成功",null);
        }
        return new Result(5000,"修改失败",null);
    }

    @RequestMapping("deleteById")
    @ApiOperation("删除评论信息")
    @ApiImplicitParam(name = "id", value = "评论编号", dataType = "String")
    public Result deleteById(String id){
        int row = ubCommentService.deleteById(id);
        if (row > 0){
            return new Result(2000,"删除成功",null);
        }
        return new Result(5000,"删除失败",null);
    }

    @RequestMapping("findMyComment/{id}/{pageCurrent}/{pageSize}")
    @ApiOperation("查询评论信息，含分页以及条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageCurrent", value = "页码", dataType = "int" , defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", dataType = "int" , defaultValue = "5"),
            @ApiImplicitParam(name = "id", value = "商品id", dataType = "int"),
    })
    public Result<PageInfo<Comment>> findMyComment(@PathVariable("id") String id,
                                               @PathVariable("pageCurrent") Integer pageCurrent,
                                               @PathVariable("pageSize")Integer pageSize){
        PageInfo<Comment> comments = ubCommentService.findMyComment(pageCurrent,pageSize,id);
        return new Result<>(2000,"查询成功",comments);
    }

    @RequestMapping("getCommentById/{id}")
    @ApiOperation("根据id查询评论信息")
    @ApiImplicitParam(name = "id", value = "评论编号", dataType = "String")
    public Result<Comment> getCommentById(@PathVariable String id){
        Comment comment = ubCommentService.getById(id);
        if (comment != null){
            return new Result(2000,"查询成功",comment);
        }
        return new Result(5000,"查询失败",null);
    }
}
